package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.AttrAttrgroupRelationMapper;
import com.bush.mall.product.entity.AttrAttrgroupRelationEntity;
import com.bush.mall.product.service.AttrAttrgroupRelationService;

/**
 * 属性&属性分组关联
 * @author RUN
 * @date 2022-11-28
 */
@Service(AttrAttrgroupRelationService.BEAN_ID)
@Slf4j
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationMapper, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    /**
     * 查询属性&属性分组关联列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<AttrAttrgroupRelationEntity> page = new Page<>(current,size);
            QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("AttrAttrgroupRelationService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建属性&属性分组关联表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveAttrAttrgroupRelation(AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        try{
            Assert.notNull(attrAttrgroupRelation,"参数异常");
            boolean flag = this.save(attrAttrgroupRelation);
            if (flag) return Result.ok("属性&属性分组关联添加成功");
            throw new SaaSException("属性&属性分组关联添加失败");
        }catch(Exception e){
            log.error("AttrAttrgroupRelationService saveAttrAttrgroupRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改属性&属性分组关联表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateAttrAttrgroupRelation(AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        try{
            Assert.notNull(attrAttrgroupRelation,"参数异常");
            boolean flag = this.updateById(attrAttrgroupRelation);
            if (flag) return Result.ok("属性&属性分组关联修改成功");
            throw new SaaSException("属性&属性分组关联修改失败");
        }catch(Exception e){
            log.error("AttrAttrgroupRelationService updateAttrAttrgroupRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除属性&属性分组关联表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteAttrAttrgroupRelation(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("属性&属性分组关联删除成功");
            throw new SaaSException("属性&属性分组关联删除失败");
        }catch(Exception e){
            log.error("AttrAttrgroupRelationService deleteAttrAttrgroupRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}